package com.demo.youxuanmall.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.demo.youxuanmall.entity.Product;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface ProductMapper extends BaseMapper<Product> {
    
    /**
     * 获取热门商品
     */
    @Select("SELECT p.id, p.name, p.price, SUM(oi.quantity) as sales, " +
            "SUM(oi.total_price) as sales_amount, p.main_image as image " +
            "FROM order_item oi " +
            "JOIN product p ON oi.product_id = p.id " +
            "JOIN `order` o ON oi.order_id = o.id " +
            "WHERE o.status != 50 " +  // 50表示已取消的订单
            "GROUP BY p.id " +
            "ORDER BY sales DESC " +
            "LIMIT #{limit}")
    List<Map<String, Object>> getHotProducts(int limit);
} 